/*
 * geliftcp.cpp
 *
 *  Created on: 2011-10-21
 *  Author    : gelif(admin@gelif.net)
 */

#include "geliftcp.h"

TCPConnection::TCPConnection(GeliFrame *geliFrame)
        : wxTCPConnection()
{
    this->geliFrame = geliFrame;
}

TCPConnection::~TCPConnection()
{
}

bool TCPConnection::OnDisconnect()
{
    return true;
}

bool TCPConnection::Poke(const wxString& item, wxChar *data, int size, wxIPCFormat format)
{
    return wxTCPConnection::Poke(item, data, size, format);
}

bool TCPConnection::OnPoke(const wxString& topic, const wxString& item, wxChar *data, int size, wxIPCFormat format)
{
    if (item == "move")
    {
        GelifMove* move = new GelifMove(data);
        this->geliFrame->game->move(move->x, move->y, move->status);
        this->geliFrame->game->is_my_turn = true;
    }
    return wxTCPConnection::OnPoke(topic, item, data, size, format);
}

void TCPConnection::sendBoard()
{
    //TODO
}

void TCPConnection::sendServerList()
{
    //TODO
}

TCPClient::TCPClient(GeliFrame* geliFrame) : wxTCPClient()
{
    this->geliFrame = geliFrame;
    connection = NULL;
}

bool TCPClient::connect(const wxString& str_host, const wxString& str_service, const wxString& topic)
{
    connection = (TCPConnection *)MakeConnection(str_host, str_service, topic);

    return connection != NULL;
}

TCPClient::~TCPClient()
{
    disconnect();
}

wxConnectionBase *TCPClient::OnMakeConnection()
{
    return new TCPConnection();
}


void TCPClient::pokeServer(const wxString& item, wxChar *data, int size, wxIPCFormat format)
{
    if (connection != NULL)
    {
        connection->Poke(item, data, size, format);
    }
}

void TCPClient::disconnect()
{
    if (connection != NULL)
    {
        connection->Disconnect();
        wxDELETE(connection);
    }
}

TCPServer::TCPServer(GeliFrame* geliFrame) : wxTCPServer()
{
    this->geliFrame = geliFrame;
    connection = NULL;
}

TCPServer::~TCPServer()
{
    disconnect(); // dico also deletes
}

wxConnectionBase *TCPServer::OnAcceptConnection(const wxString& topic)
{
    //throw topic; // just for testing
    // if the connector has the right topic and we're not full, go for it!
    if (topic.Lower() == wxT("gelifconnect"))
    {
        // make sure the new connection object has a handle on the frame
        // and on its own index
        connection = new TCPConnection(geliFrame);
        geliFrame->game->status = Game::IS_ON;
        geliFrame->game->is_my_turn = true;
        //throw "canbe connceted"; // just for testing
        return connection;
    }
    else
    {
        //throw "can not be connceted";
        return NULL;
    }
}

void TCPServer::updateClient()
{
}

void TCPServer::disconnect()
{
    if (connection != NULL)
    {
        connection->Disconnect();
        wxDELETE(connection);
    }
}
/*
void TCPServer::sendServerList()
{
    connection->sendServerList();
}
*/
void TCPServer::pokeClient(const wxString& item, wxChar *data, int size, wxIPCFormat format)
{
    if (connection != NULL)
    {
        connection->Poke(item, data, size, format);
    }
}
